#include <stdio.h>
#include <stdlib.h>
// #include <conio.h>
#include <string.h>

#define getpch(type) (type *)malloc(sizeof(type))
#define UserNumber 10
#define UserFNumber 10
#define UserOFNumber 5

struct fname
{
    char fname[1];
    int flag;
} fnameA[26] = {'a', 0, 'b', 0, 'c', 0, 'd', 0, 'e', 0, 'f', 0, 'g', 0, 'h', 0, 'i', 0, 'j', 0,
                'k', 0, 'l', 0, 'm', 0, 'n', 0, 'o', 0, 'p', 0, 'q', 0, 'r', 0, 's', 0, 't', 0, 'u', 0, 'v', 0, 'w', 0,
                'x', 0, 'y', 0, 'z', 0};

struct afd
{
    char opname[10];
    int flag;
    char opfprotect[3];
    int rwpoint;
} AFD[UserFNumber];

typedef struct
{
    char fname[10];
    int flag;
    int fprotect[3];
    int flength;
} ufd, UF[UserFNumber];

struct mdf
{
    char uname[10];
    UF Udir;
} UFD[UserNumber];

void intFSystem()
{
    int i, j, k, l;
    strcpy(UFD[0].uname, "a");
    strcpy(UFD[1].uname, "b");
    strcpy(UFD[2].uname, "c");
    strcpy(UFD[3].uname, "d");
    strcpy(UFD[4].uname, "e");
    strcpy(UFD[5].uname, "f");
    strcpy(UFD[6].uname, "g");
    strcpy(UFD[7].uname, "h");
    strcpy(UFD[8].uname, "i");
    strcpy(UFD[9].uname, "j");
    for (i = 0; i < 10; i++)
    {
        for (k = 0; k < 5; k++)
        {
            do
                j = rand() % 26;
            while (fnameA[j].flag);
            strcpy(UFD[i].Udir[k].fname, fnameA[j].fname);
            fnameA[j].flag = 1;
            UFD[i].Udir[k].flength = rand() % 2048 + 1;
            UFD[i].Udir[k].flag = 1;
            UFD[i].Udir[k].fprotect[0] = rand() % 2;
            UFD[i].Udir[k].fprotect[1] = rand() % 2;
            UFD[i].Udir[k].fprotect[2] = rand() % 2;
        }
        for (j - 0; j < 26; j++)
            fnameA[j].flag = 0;
    }
    for (l = 0; l < 5; i++)
    {
        strcpy(AFD[i].opname, "");
        AFD[l].flag = 0;
        AFD[l].opfprotect[0] = 0;
        AFD[l].opfprotect[1] = 0;
        AFD[l].opfprotect[2] = 0;
        AFD[l].rwpoint = 0;
    }
}

void printUFD(int i)
{
    int k;
    printf("主文件目录:\n", UFD[i].uname);
    printf("用户名\t文件名\t可读可写可执行\t文件长度\n");
    for (k = 0; k < 10; k++)
    {
        if (UFD[i].Udir[k].flag)
            printf("%s\t%s\t%d %d %d\t\t%dB\n", UFD[i].uname, UFD[i].Udir[k].fname, UFD[i].Udir[k].fprotect[0],
                   UFD[i].Udir[k].fprotect[1], UFD[i].Udir[k].fprotect[2], UFD[i].Udir[k].flength);
    }
}

void printAFD()
{
    int l;
    printf("运行文件目录:\n");
    printf("文件名\t可读可写可执行\n");
    for (l = 0; l < 5; l++)
    {
        if (AFD[l].flag)
            printf("%s\t%d%d%d\n", AFD[l].opname, AFD[l].opfprotect[0], AFD[l].opfprotect[1], AFD[l].opfprotect[2]);
    }
}

void Create(int i)
{
    int k;
    for (k = 0; k < 10; k++)
    {
        if (!UFD[i].Udir[k].flag)
            break;
    }
    if (k >= 10)
    {
        printf("一个用户不能拥有超过10个文件\n\n");
        return;
    }
    printf("请输入你想创建的文件名:");
    scanf("%s", UFD[i].Udir[k].fname);
    printf("请输入文件长度:");
    scanf("%d", &UFD[i].Udir[k].flength);
    printf("可读?(lyes,Ono):");
    scanf("%d", &UFD[i].Udir[k].fprotect[0]);
    printf("可写?(lyes,Ono):");
    scanf("%d", &UFD[i].Udir[k].fprotect[1]);
    printf("可执行?(lyes,Ono):");
    scanf("%d", &UFD[i].Udir[k].fprotect[2]);
    UFD[i].Udir[k].flag = 1;
    return;
}

void Delete(int i)
{
    char file[10];
    int k;
    printf("请输入你想删除的文件名:");
    scanf("%s", file);
    for (k = 0; k < 10; k++)
    {
        if (UFD[i].Udir[k].flag && !strcmp(UFD[i].Udir[k].fname, file))
        {
            UFD[i].Udir[k].flag = 0;
            break;
        }
        else
            printf("出错\n");
    }
    return;
}

void Open(int i)
{
    int l, k, n;
    char file[10];
    for (l = 0; l < 5; l++)
    {
        if (!AFD[l].flag)
            break;
    }
    printf("请输入你想打开的文件名:");
    scanf("%s", file);
    for (n = 0; n < 5; n++)
        if (!strcmp(AFD[n].opname, file) && AFD[n].flag)
        {
            printf("the file had opened!\n", file);
            return;
        }
    for (k = 0; k < 10; k++)
        if (!strcmp(UFD[i].Udir[k].fname, file) && UFD[i].Udir[k].flag)
        {
            strcpy(AFD[l].opname, UFD[i].Udir[k].fname);
            AFD[l].opfprotect[0] = UFD[i].Udir[k].fprotect[0];
            AFD[l].opfprotect[1] = UFD[i].Udir[k].fprotect[1];
            AFD[l].opfprotect[2] = UFD[i].Udir[k].fprotect[2];
            AFD[l].flag = 1;
            printf("文件已打开!\n");
            return;
        }
    printf("文件不存在!\n");
    return;
}

void Close()
{
    int l;
    char file[10];
    printf("请选择文件:");
    scanf("%s", file);
    for (l = 0; l < 5; l++)
        if (!strcmp(AFD[l].opname, file) && AFD[l].flag)
        {
            AFD[l].flag = 0;
            printf("the file has close\n");
            return;
        }
    if (l >= 5)
        printf("文件打开失败!\n");
    return;
}

void Read()
{
    int l;
    char file[10];
    printf("请输入你想读的文件:");
    scanf("%s", file);
    for (l = 0; l < 5; l++)
        if (!strcmp(AFD[l].opname, file) && AFD[l].flag)
            if (AFD[l].opfprotect[0])
            {
                printf("the file has read.\n");
                return;
            }
            else
            {
                printf("can not read!\n");
                return;
            }
    if (l >= 5)
        printf("文件尚未打开，请先打开文件n");
    return;
}

void Write()
{
    int l;
    char file[10];
    printf("请输入你想写的文件:");
    scanf("%s", file);
    for (l = 0; l < 5; l++)
        if (!strcmp(AFD[l].opname, file) && AFD[l].flag)
            if (AFD[l].opfprotect[l])
            {
                printf("the file has written.\n");
                return;
            }
            else
            {
                printf("can not write!\n");
                return;
            }
    if (l >= 5)
        printf("文件尚未打开，请先打开文件\n");
    return;
}

void main()
{
    int i, n = 0;
    char m[10];
    char login[10];
    int FSystem();
    printf("欢迎使用\n");
    printf("1.本系统模拟文件管理\n2.系统已初始化10个用户，\n用户名分别是a~j，每个用户已分配五个文件\n");
    printf("**本系统的命令如下:\n");
    printf("**创建文件(create)\n");
    printf("**删除文件(delete)\n");
    printf("**打开文件(open)\n");
    printf("**关闭文件(close)\n");
    printf("**读取文件(read)\n");
    printf("**编写文件(write)\n");
    printf("**显示文件目录(printufd)\n");
    printf("**显示打开文件目录(dir)\n");
    printf("**退出(exit)\n");
lgin:printf("请输入用户名(a~j):");
    scanf("%s", login);
    if (!strcmp(login, "exit"))
        return;
    for (i = 0; i < 10; i++)
    {
        if (!strcmp(UFD[i].uname, login))
            break;
    }
    if (i >= 10)
    {
        printf("该用户名不存在!");
        goto lgin;
    }
    printUFD(i);
    for (; n != 1;)
    {
        printf("请输入命令:");
        scanf("%s", &m);
        if (strcmp(m, "create") == 0)
            Create(i);
        else if (strcmp(m, "delete") == 0)
            Delete(i);
        else if (strcmp(m, "open") == 0)
            Open(i);
        else if (strcmp(m, "close") == 0)
            Close;
        else if (strcmp(m, "read") == 0)
            Read();
        else if (strcmp(m, "write") == 0)
            Write();
        else if (strcmp(m, "printufd") == 0)
            printUFD(i);
        else if (strcmp(m, "exit") == 0)
            n = 1;
        else
            printf("出错\n");
    }
    printf("Saving....\n");
    printUFD(i);
    system("stty -echo");
    getchar();
    system("stty echo");
    return;
}